using System;
using System.Collections.Generic;
using System.Text;
using PokerServerWebService.DataContracts;

namespace PokerServerWebService.BusinessLogic
{
    class CardDeck
    {
        private Dictionary<int, Card> cards;

        public CardDeck()
        {
            cards = new Dictionary<int, Card>( );
            int key = 0;
            for ( int i = 0; i < 4; i++ )
                for ( int j = 0; j < 13; j++ )
                {
                    cards.Add( key, TranslateIntIntTotmpCard( j, i ) );
                    key++;
                }
        }

        private void SortDeck( int seed )
        {
            List<int> keys = new List<int>( );
            List<int> positions = new List<int>( );
            Random rand = new Random( seed );
            Dictionary<int, Card> newDeck = new Dictionary<int, Card>( );

            for ( int i = 0; i < 52; i++ )
            {
                keys.Add( i );
                positions.Add( i );
            }

            while ( keys.Count != 0 )
            {
                DateTime x = DateTime.Now;
                rand = new Random( x.Millisecond * x.Second * x.Minute * x.Day );

                int key = rand.Next( x.Hour * x.Millisecond ) % keys.Count;
                System.Threading.Thread.Sleep( 1 );
                int pos = rand.Next( x.Day * x.Second ) % positions.Count;
                key = keys[key];
                pos = positions[pos];

                if ( keys.Contains( key ) && positions.Contains( pos ) )
                {
                    Card tmp = new Card( );
                    if ( !cards.TryGetValue( key, out tmp ) )
                        throw new Exception( "shuffle eror" );
                    cards.Remove( key );
                    keys.Remove( key );
                    positions.Remove( pos );
                    newDeck.Add( pos, tmp );
                }
            }
            cards.Clear( );
            cards = newDeck;
        }


        private Card TranslateIntIntTotmpCard( int face, int color )
        {
            Card returnValue = null;

            if ( face > 12 || face < 0 || color > 3 || color < 0 )
                throw new ArgumentException( );

            CardColor cc = new CardColor( );
            CardFace cf = new CardFace( );

            switch ( color )
            {
                case 0: cc = CardColor.Clubs; break;
                case 1: cc = CardColor.Spades; break;
                case 2: cc = CardColor.Diamonds; break;
                case 3: cc = CardColor.Hearts; break;
            }

            switch ( face )
            {
                case 0: cf = CardFace.two; break;
                case 1: cf = CardFace.three; break;
                case 2: cf = CardFace.four; break;
                case 3: cf = CardFace.five; break;
                case 4: cf = CardFace.six; break;
                case 5: cf = CardFace.seven; break;
                case 6: cf = CardFace.eight; break;
                case 7: cf = CardFace.nine; break;
                case 8: cf = CardFace.ten; break;
                case 9: cf = CardFace.Jack; break;
                case 10: cf = CardFace.Queen; break;
                case 11: cf = CardFace.King; break;
                case 12: cf = CardFace.Ace; break;
            }
            returnValue = new Card( cf, cc );
            return returnValue;
        }

        public List<Card> GetShuffledDeck(int seed)
        {
            SortDeck( seed );
            List<Card> tmp = new List<Card>( );
            for ( int i = 0; i < 52; i++ )
            {
                tmp.Add( cards[i]);
            }

            return tmp;
        }
    }
}
